import java.util.*;
import java.awt.*;

import javax.swing.JOptionPane;

public class Player extends Observable implements Observer{
    Observable Subject;
    private String Name;
    private String Password;
    private int TechnologyLevel;
    private Color Hue;
    private ArrayList<Territory> TerritoryList;
    private int Food;
    private int Technology;
    private String[] TechnologyName = {"Infantry", "Automatic Weapons", "Rocket Launchers", "Tanks", "Improved Tanks", "Fighter Planes"};

    public Player(String _Name , String _Password, Color _Hue){
        Name = _Name;
        Password = _Password;
        TechnologyLevel = 4;
        Technology = 100;
        Food = 0;
        Hue = _Hue;
        TerritoryList = new ArrayList<Territory>();
     }
   
    public void update(Observable obs, Object ownerChanged){
    	Territory t =(Territory)obs;
    	if ((boolean)ownerChanged){
    		if (TerritoryList.contains(t))
    			TerritoryList.remove(t);
    		else TerritoryList.add(t);
    	    measurementsChanged();
    	}
    }
    
    public void measurementsChanged(){
    	setChanged();
    	notifyObservers();//pull method
    }
    
    public String getName(){return Name;}
    public void setName(String n){Name = n;}
    public String getPassword(){return Password;}
    public void setPassword(String _Password){Password = _Password;}
    public Color getHue(){return Hue;}
    public void setHue(Color c){Hue = c;}
    
    public int getTroopAmount(){
    	int TroopAmount = 0;
    	for (Territory t: TerritoryList)
    		TroopAmount += t.getTroopAmount();
    	return TroopAmount;
    }
    
    public int getTroopAmount(int index){
    	int TroopAmount = 0;
    	for (Territory t: TerritoryList)
    		TroopAmount += t.getTroopAmount(index);
    	return TroopAmount;
    }
    
    public int getFood(){return Food;}
    public int getFoodUnit(){
    	int FoodUnit = 0;
    	for (Territory t : TerritoryList)
    		FoodUnit += t.getFoodUnit();
    	return FoodUnit;
    }
    public int getTechnology(){return Technology;}
    public int getTechnologyUnit(){
    	int TechnologyUnit = 0;
    	for (Territory t: TerritoryList)
    		TechnologyUnit += t.getTechnologyUnit();
    	return TechnologyUnit;
    }
    public void cutTechnology(int n){
    	Technology -= n;
    }
    
    public int getTechnologyLevel(){return TechnologyLevel;}
    public String getTechnologyLevelName(){return TechnologyName[TechnologyLevel];};
    public void upgradeTechnologyLevel(){
    	TechnologyLevel ++;
    }

    public ArrayList<Territory> getTerritoryList(){return TerritoryList;}
    public Territory getTerritoryList(int index){return TerritoryList.get(index);}
    public int getTerritoryAmount(){return TerritoryList.size();}
   
    public void feed(){
    	Food += getFoodUnit();
    	Technology += getTechnologyUnit();
    	if (Food >= getTroopAmount() + getTerritoryAmount()) {
    		Food -= getTroopAmount() + getTerritoryAmount();
    		for (Territory t: TerritoryList)
    			t.addTroop(new Troop(0, false));
    		return;
        }// enough food for all the troops, every territory can get one new infantry
    	if (Food >= getTroopAmount()){
    		Food -= getTroopAmount();
    		return;
    	}// enough food for all the troops, no new infantry
    	int diff = getTroopAmount() - Food;// no enough food, diff troops must die
    	while (diff > 0){
    	    int t = 0;
    	    int n = 0;
    	    while (getTroopAmount(t) == 0)//no troops for this type
    	       t++;//check next type
    	    do {
    		   n = (int)Math.random() * getTerritoryAmount();
    	    }while (TerritoryList.get(n).getTroopAmount(t) == 0);// random choose a territory if it has specific type of troop 
    	    JOptionPane.showMessageDialog(null, TerritoryList.get(n).getLocation() + "One of your " + TechnologyName[t] + "starved to death!");
    	    TerritoryList.get(n).removeTroop(t);// remove one troop
            diff --;
    	}
    	measurementsChanged();
    }


}
    	
    	   

